package com.github.elliot.usercenter.controller;

import com.github.elliot.domain.UserVO;
import com.github.elliot.usercenter.po.User;
import com.github.elliot.usercenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@RestController
@RequestMapping
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/api/v1/users")
    @ResponseStatus(HttpStatus.OK)
    public Flux<User> getAllUser() {
        return userService.findAll();
    }

    @GetMapping("/api/v1/users/page")
    @ResponseStatus(HttpStatus.OK)
    public Mono<Page<User>> getPageUser(@RequestParam("current") Integer current, @RequestParam("size") Integer size) {
        return userService.pageListService(current, size);
    }

    @GetMapping("/api/v1/user/{userNo}")
    @ResponseStatus(HttpStatus.OK)
    public Mono<User> getUserByUserNo(@PathVariable(value = "userNo") String userNo) {
        return userService.findByUserNo(userNo);
    }

    @GetMapping("/api/v1/user/username/{username}")
    @ResponseStatus(HttpStatus.OK)
    public Mono<UserVO> getUserByUsername(@PathVariable(value = "username") String username) {
        return userService.getUserByUsername(username);
    }

    @PostMapping("/api/v1/user")
    @ResponseStatus(HttpStatus.OK)
    public Mono<User> save(@RequestBody User user){
        return userService.save(user);
    }

    @PostMapping("/api/v1/users")
    @ResponseStatus(HttpStatus.OK)
    public Flux<User> saveUsers(@RequestBody List<User> userList){
        return userService.saveUsers(userList);
    }

    @DeleteMapping("/api/v1/user/{userNo}")
    @ResponseStatus(HttpStatus.OK)
    public Mono<Void> deleteByUserNo(@PathVariable(value = "userNo") String userNo){
        return userService.deleteByUserNo(userNo);
    }

}
